/**
  ******************************************************************************
  * File Name          : fstsk.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "ff.h"
#include <string.h>

TaskHandle_t xFs_Thd = NULL;
StaticTask_t xFs_Tcb;
StackType_t  xFs_Stk[FS_STACK_SIZE];

FATFS fs;
FIL   fil;
FIL   fil2;

static const char fs_lable[]  = "";
static const char file_name[] = "prod_his";

char test[] = "This is a write test!ABCdef0123!@$^\r\n";

void vFs_Task(void * pvParameters)
{
  uint8_t i;
  FRESULT res;
  UINT br;

  res = f_mount(&fs, fs_lable, 1);
  if(res != FR_OK)
  {
    FsErrLog("mount res:%d\r\n", res);
    while(1)
    {
      vTaskDelay(1000);
    }
  }

  res = f_open(&fil2, file_name, FA_OPEN_APPEND | FA_WRITE);
  for(i=0; i<200; i++)
  {
    FsErrLog("f_open FA_OPEN_ALWAYS res:%d\r\n", res);
    if(res == FR_OK)
    {
      res = f_write(&fil2, test, strlen(test), &br);
      FsErrLog("f_write res:%d, bw:%d\r\n", res, br);
    }
  }
  f_close(&fil2);

  while(1)
  {
    vTaskDelay(1000);
  }
}
